package com.barbeque.mall.service;

import cn.hutool.core.util.RandomUtil;
import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.advertise.BO.CouponListBo;
import com.barbeque.mall.bean.advertise.BO.CouponListUserBo;
import com.barbeque.mall.bean.advertise.MarketCoupon;
import com.barbeque.mall.bean.advertise.MarketCouponExample;
import com.barbeque.mall.bean.advertise.MarketCouponUser;
import com.barbeque.mall.bean.advertise.MarketCouponUserExample;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketCouponMapper;
import com.barbeque.mall.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author bai
 * @since 2022/07/17 02:03
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;


    /**
     * 显示优惠券列表的逻辑
     * @return
     */
    @Override
    public CommonData<MarketCoupon> queryCouponList(CouponListBo couponListBo) {
        PageHelper.startPage(couponListBo.getPage(),couponListBo.getLimit());
        // 查到信息，存入list
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(null);
        PageInfo<MarketCoupon> marketCouponPageInfo = new PageInfo<>(marketCoupons);

        return CommonData.data(marketCouponPageInfo);
    }

    /**
     * 添加优惠券信息的逻辑
     * @param
     * @return
     */
    @Override
    public MarketCoupon insertCouponInfo(MarketCoupon marketCoupon) {
        // 判断增加的优惠券类型
        // 如果type为2，则代表需要随机生成一个兑换码
        if(marketCoupon.getType() == 2){
            String upperStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            String numStr="1234567890";
            String randomString = "";

            // 随机生成兑换码的逻辑DC6FF8SA
            int randomInt = RandomUtil.randomInt(8,9);
            randomString = RandomUtil.randomString(upperStr + numStr,randomInt);
            marketCoupon.setCode(randomString);
        }

        // 添加时间
        Date date = new Date();
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);

        // 插入一条数据
        marketCouponMapper.insertSelective(marketCoupon);
        // 根据id，将插入的数据查询出来
        marketCoupon = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());



        return marketCoupon;

    }

    /**
     * 更新优惠券信息的逻辑
     * @param marketCoupon
     * @return
     */
    @Override
    public MarketCoupon updateCouponInfo(MarketCoupon marketCoupon) {
        // 更新操作
       marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
       // 将更新的数据查出来，作为返还给前端的数据
       marketCoupon = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());

        Date date = new Date();
        marketCoupon.setUpdateTime(date);
        return marketCoupon;
    }

    /**
     * 删除一条优惠券信息的逻辑
     * @param marketCoupon
     */
    @Override
    public void deleteCouponInfo(MarketCoupon marketCoupon) {
        marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
    }

    /**
     * 根据id查询优惠券信息的逻辑(详情)
     * @param id
     * @return
     */
    @Override
    public MarketCoupon queryCouponInfoById(Integer id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);

        return marketCoupon;
    }

    /**
     * 根据优惠券id查询持有此优惠券的用户
     * @param couponListUserBo
     * @return
     */
    @Override
    public CommonData<MarketCouponUser> querryCouponListUserByCouponId(CouponListUserBo couponListUserBo) {
        PageHelper.startPage(couponListUserBo.getPage(),couponListUserBo.getLimit());

        // 条件查询
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponListUserBo.getCouponId());
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);


        PageInfo<MarketCouponUser> marketCouponUserPageInfo = new PageInfo<>(marketCouponUsers);

        return CommonData.data(marketCouponUserPageInfo);
    }

    /**
     * 优惠券的模糊查询逻辑
     * @param couponListBo
     * @return
     */
    @Override
    public CommonData<MarketCoupon> queryCouponListByCondition(CouponListBo couponListBo) {
        PageHelper.startPage(couponListBo.getPage(),couponListBo.getLimit());

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if(couponListBo.getName() != null){
            criteria.andNameLike("%" + couponListBo.getName() + "%");
        }
        if(couponListBo.getStatus() != null){
            criteria.andStatusEqualTo(couponListBo.getStatus());
        }
        if(couponListBo.getType() != null){
            criteria.andGoodsTypeEqualTo(couponListBo.getType());
        }

        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);

        PageInfo<MarketCoupon> marketCouponPageInfo = new PageInfo<>(marketCoupons);

        return CommonData.data(marketCouponPageInfo);
    }

    /**
     * 对优惠券持有人的模糊查询操作
     * @param couponListUserBo
     * @return
     */
    @Override
    public CommonData<MarketCouponUser> querryCouponListUserCondition(CouponListUserBo couponListUserBo) {
        PageHelper.startPage(couponListUserBo.getPage(),couponListUserBo.getLimit());

        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        if(couponListUserBo.getUserId() != null){
            criteria.andUserIdEqualTo(couponListUserBo.getUserId());
        }
        if(couponListUserBo.getStatus() != null){
            criteria.andStatusEqualTo(couponListUserBo.getStatus());
        }

        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);

        PageInfo<MarketCouponUser> marketCouponUserPageInfo = new PageInfo<>(marketCouponUsers);

        return CommonData.data(marketCouponUserPageInfo);
    }

}
